#include "../engine/protocol_analyzer_impl.hh"
#include "unittest/unittest.hh"
#include "../corelib/network/network.hh"
#include <cstring>
#include <cstdlib>
#include <chrono>

#ifndef WIN32
#include <arpa/inet.h>
#endif

using namespace kratos::engine;
using namespace kratos::network;

FIXTURE_BEGIN(ProtocolAnalyzer)

CASE(TestSetAnalyzer) {
    ProtocolAnalyzerImpl analyzer;
    ASSERT_TRUE(analyzer.setAnalyzer("2l:2:4"));
    ASSERT_TRUE(analyzer.setAnalyzer("2:2l:4"));
    ASSERT_TRUE(analyzer.setAnalyzer("4:2:4L"));
    ASSERT_FALSE(analyzer.setAnalyzer("4l:2:4L"));
    ASSERT_FALSE(analyzer.setAnalyzer("5l:2:4"));
}

CASE(TestHasFullPacket) {
    ProtocolAnalyzerImpl analyzer;
    analyzer.setAnalyzer("1:2l:3");
    ASSERT_TRUE(analyzer.isParsed());
    char buffer[14] = { 0 };
    auto u16_ptr = reinterpret_cast<uint16_t*>(buffer+1);
    *u16_ptr = htons(8);
    SessionReader reader;
    reader.push(buffer, sizeof(buffer));
    std::size_t size = 0;
    ASSERT_TRUE(analyzer.hasFullPacket(reader, size));
    ASSERT_TRUE(sizeof(buffer) == size);
}

FIXTURE_END(ProtocolAnalyzer)
